/**
 * Created by Administrator on 2017/6/17.
 */

let userDao = require('../mysql/userDao.js'); 
let log = require('../util/log.js');
let create = require('./create.js');
let rpc = require('../util/rpc.js');

module.exports = lovelive;

function lovelive(uid,groupid,platform) {
    this._token = null;
    this._expire = 0;
    this._platform = platform;
    this._uid = uid;
    this._groupid = groupid;
    this._name = null;
    this._avatar = null;
    this._cb = null;
}

lovelive.prototype.run = function (cb) {
    this._cb = cb;
    this.self();
};

lovelive.prototype.self = function () { 
    let _this = this; 
    _this._name = ""+this._uid;
    _this._avatar = ""; 
    let userModel = new userDao();
    userModel.inLoveLiveId(_this._uid, function (err, res) {
        if (res == null) {
            _this.reg();
        } else {
            let param = {};
            param.name = _this._name;
            param.avatar = _this._avatar;
            create.reToken(userModel, param, _this._cb);
        }   
    });  
};

lovelive.prototype.reg = function () {
    let _this = this;
    create.lovelive(this._uid,this._groupid,this._name, this._avatar, function (err, res, token) {
        if (res == null) {
            _this._cb(null, {'error': 105, 'msg': '创建账号失败'});
            return false;
        }
        _this._cb(null, {'token': token});
    });
};